package com.efounder.widget.skinview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ListView;

import com.efounder.thirdpartycomps.R;
import com.efounder.utils.JfResourceUtil;

import skin.support.widget.SkinCompatSupportable;

import static skin.support.widget.SkinCompatHelper.INVALID_ID;

/**
 * Listview 换肤支持
 * Created by yqs on 2019/05/20
 */
public class SkinListView extends ListView implements SkinCompatSupportable {
    private Drawable divider;
    private int dividerHeight = 0;

    public SkinListView(Context context) {
        this(context, null);
    }

    public SkinListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SkinListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = null;
        try {
            ta = context.obtainStyledAttributes(attrs, R.styleable.SkinDividerHelper);
            if (ta.hasValue(R.styleable.SkinDividerHelper_android_divider)) {
                int dividerId = ta.getResourceId(R.styleable.SkinDividerHelper_android_divider, INVALID_ID);
                divider = JfResourceUtil.getSkinDrawable(dividerId);
                if (divider != null) {
                    setDivider(divider);
                }
            }

            if (ta.hasValue(R.styleable.SkinDividerHelper_android_dividerHeight)) {
                dividerHeight = ta.getDimensionPixelSize(
                        R.styleable.SkinDividerHelper_android_dividerHeight, 0);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ta != null) {
                ta.recycle();
            }
        }
        applySkin();

    }


    @Override
    public void applySkin() {
        if (divider != null) {
            setDivider(divider);
        }
        if (dividerHeight != 0) {
            setDividerHeight(dividerHeight);
        }
    }

}
